package org.dromara.web.controller.app;


import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.mongo.ChatMessage;
import org.dromara.common.mongo.ChatMessageMongoDao;
import org.dromara.common.mongo.Session;
import org.dromara.common.mongo.SessionMongoDao;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.dromara.web.controller.app.req.SessionReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 *  会话相关
 *
 */
@Slf4j
@RestController
@RequestMapping("/app/session")
public class SessionController extends BaseController {

    @Autowired
    ChatMessageMongoDao chatMessageMongoDao;
    @Autowired
    SessionMongoDao sessionMongoDao;

    /**
     *  获取会话列表记录
     *
     * @return
     */
    @Operation
    @Parameter(name = "agentId", description = "智能体ID， 获取智能体下的记录列表时使用")
    @GetMapping("/history")
    public R<List<Session>> sessionList(Long agentId) {
        Long userId = LoginHelper.getAppUserId();
        return R.ok(sessionMongoDao.listSession(userId, agentId));
    }


    /**
     *  删除会话
     *
     * @param req
     * @return
     */
    @Operation
    @PostMapping("/remove")
    public R<Void> deleteSession(@RequestBody SessionReq req) {
        sessionMongoDao.deleteSession(req.getSessionId());
        return R.ok();
    }

    /**
     *  会话记录
     *
     * @param sessionId
     * @return
     */
    @GetMapping("/messages")
    public R<List<ChatMessage>> messageList(String sessionId) {
        Long userId = LoginHelper.getAppUserId();
        return R.ok(chatMessageMongoDao.getMessages(userId, sessionId));
    }



}
